import { Module, Global } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MonitoringService } from './monitoring.service';
import {
  MonitoringInterface,
  HealthCheckInterface,
} from './monitoring.interface';

/**
 * 监控模块
 * 基于 NestJS 官方示例实现
 * 参考: https://docs.nestjs.cn/fundamentals/dependency-injection
 * 对应 Java: 监控配置
 */
@Global()
@Module({
  imports: [ConfigModule],
  providers: [
    {
      provide: 'MONITORING_PROVIDER',
      useFactory: (configService: ConfigService) => {
        // 这里会根据配置选择具体的监控实现
        // 默认使用 Prometheus，也可以使用其他监控系统
        const monitoringType = configService.get(
          'monitoring.type',
          'prometheus',
        );

        if (monitoringType === 'prometheus') {
          // 返回 Prometheus 监控实现
          return {
            async incrementCounter(
              name: string,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Counter ${name} incremented`,
                labels || '',
              );
            },
            async setGauge(
              name: string,
              value: number,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Gauge ${name} set to ${value}`,
                labels || '',
              );
            },
            async recordHistogram(
              name: string,
              value: number,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Histogram ${name} recorded ${value}`,
                labels || '',
              );
            },
            async recordSummary(
              name: string,
              value: number,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Summary ${name} recorded ${value}`,
                labels || '',
              );
            },
          };
        } else {
          // 返回其他监控实现
          return {
            async incrementCounter(
              name: string,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Counter ${name} incremented`,
                labels || '',
              );
            },
            async setGauge(
              name: string,
              value: number,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Gauge ${name} set to ${value}`,
                labels || '',
              );
            },
            async recordHistogram(
              name: string,
              value: number,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Histogram ${name} recorded ${value}`,
                labels || '',
              );
            },
            async recordSummary(
              name: string,
              value: number,
              labels?: Record<string, string>,
            ): Promise<void> {
              console.log(
                `[MONITORING] Summary ${name} recorded ${value}`,
                labels || '',
              );
            },
          };
        }
      },
      inject: [ConfigService],
    },
    {
      provide: 'HEALTH_CHECK_PROVIDER',
      useFactory: (configService: ConfigService) => {
        // 健康检查实现
        return {
          async checkHealth(): Promise<{ status: string; details: any }> {
            return {
              status: 'healthy',
              details: {
                timestamp: new Date().toISOString(),
                uptime: process.uptime(),
                memory: process.memoryUsage(),
                version: process.version,
              },
            };
          },
          async checkDatabase(): Promise<{ status: string; details: any }> {
            return {
              status: 'healthy',
              details: {
                timestamp: new Date().toISOString(),
                message: 'Database connection healthy',
              },
            };
          },
          async checkRedis(): Promise<{ status: string; details: any }> {
            return {
              status: 'healthy',
              details: {
                timestamp: new Date().toISOString(),
                message: 'Redis connection healthy',
              },
            };
          },
        };
      },
      inject: [ConfigService],
    },
    MonitoringService,
  ],
  exports: [MonitoringService],
})
export class MonitoringModule {}
